var prmoseHttp = require('../../utils/prmoseHttp.js');
var util = require('../../utils/util.js')
var app = getApp();
Page({
  data: {
    statusBarHeight: '',
    titleBarHeight: '',
    navHeight: '',
    bgHeight: '',
    imgHead:'https://rhi0.xiyun.net/zhuandian/image/',
    userInfo:{},
    accountInfo:{},
    isLogin:false,
    isShow:true,
    signOpened:false,
    imgSrc:'https://rhi0.xiyun.net/zhuandian/',
    userId:'',
    actOpentwo:false,
    inVita:{},
    isLink:true,
    menu1:{},
    menu2:{},
  },
  onLoad() {
    my.getSystemInfo({
      success: (res) => {
        this.setData({
          'statusBarHeight': res.statusBarHeight,//状态栏高度 
          'titleBarHeight': res.titleBarHeight,//标题栏高度
          'navHeight': res.statusBarHeight + res.titleBarHeight, //导航栏高度
          'bgHeight': res.statusBarHeight + res.titleBarHeight + 120,//背景图片高度
          menu1:util.getMenu1(),
          menu2:util.getMenu2()
        })
      }
    });
    this.getUserInfo();
    if(my.getStorageSync({key:'userInfo'}).data.id){
      let id = my.getStorageSync({key:'userInfo'}).data.id;
      this.setData({
        userId:id,
      })
    }
    my.removeStorageSync({key:'dhNum'})
    my.setStorage({key:"dhNum",data:2})
  },
  onShow(){
    let that = this;
    if(app.globalData.token){
      that.getAccount();
      that.getReward();
    }
  },
  getUserInfo(){
    let that = this;
    prmoseHttp.publicRequest(prmoseHttp.apiAddress.userInfo,{},1,'Get').then(res=>{
      if(res.code == 200){
        that.setData({
          userInfo:res.data,
          isLogin:true,
          isShow:true,
        })
        my.setStorageSync({
          key:'userInfo',
          data:res.data
        })
        that.getAccount();
      }else{
        that.setData({
          isLogin:false,
          isShow:false,
        })
      }
    })
  },
  GetAuthorize(){
    my.showLoading({
      title: '登录中'
    });
    var that = this;
    my.getAuthCode({
      scopes: 'auth_base',
      success: function (ress) {
        // console.log(ress)
        prmoseHttp.publicRequest(prmoseHttp.apiAddress.getToken,{
          appId:app.globalData.appId,
          authCode:ress.authCode,
          thirdPartyType:1,
          inviterId:app.globalData.invitaUserId,
        },1,'get').then(result => {
          if (result.code == 200) {
            app.globalData.token = result.data;
            that.goLogin();
          }
        }).catch(err => {
          console.log(err);
        });
      },
      fail: function (ress) {
        my.showToast({
          content: '授权失败',
          type: 'none'
        });
      }
    });
  },
  //执行登录
  getInfor(){
    this.getUserInfo();
  },
  //账户信息
  getAccount(){
    let that = this;
    prmoseHttp.publicRequest(prmoseHttp.apiAddress.account,{},1,'get').then(res=>{
      if(res.code == 200){
        that.setData({
          accountInfo:res.data
        })
      }else if(res.code == 408 || res.code == 405){
        that.GetAuthorize();
      }
    })
  },
  copy(){
    my.setClipboard({
      text: this.data.userInfo.id, // 剪贴板数据
      success: (res) => {
        my.alert({
          content: '复制成功！'
        });
      },
    });
  },
  getReward(){
    let that = this;
    prmoseHttp.publicRequest(prmoseHttp.apiAddress.invitaReward,{},1,'get').then(res=>{
      that.setData({
        inVita:res.data
      })
    })
  },
  showModel(){
    this.setData({
      isShow:false
    })
  },
  close(){
    this.setData({
      isShow:true
    })
  },
  advise(){
      my.navigateTo({
        url: '/pages/advise/advise'
      });     
  },
  share(){
    my.showSharePanel();
  },
  shareGift(){
    my.navigateTo({
      url: '/pages/visitGift/visitGift'
    });
    
  },
  cashExchange(){
    my.navigateTo({
      url: '/pages/cashExchange/cashExchange'
    });
  },
  remainder(){
    my.navigateTo({
      url: '/pages/remainder/remainder'
    });
  },
  onShareAppMessage () {
    return {
      title: '挣点', // 标题
      desc: '任务多多，天天领取现金红包', // 描述
      path: 'pages/index/index?userid='+this.data.userId, // 分享的小程序页面
      success(res){
        my.navigateTo({
          url: '/pages/shareSuccess/shareSuccess'
        }); 
      },
      fail(res){
        console.log('fail',res)
      },
    }
  },
});
